<script setup lang="ts">
import { ref } from 'vue'
import { useRoute } from 'vue-router'
import { timeFormat, timeFormat_2, toYuan, copyToCliboard } from '@/hooks/util'
import * as adminFinancialCenterApi from '@/api/admin'

import { message } from 'ant-design-vue'

const route = useRoute()

interface Pagination {
  page: number
  pageSize: number
  total: number
  current?: number
  showQuickJumper?: boolean
}
const handlePageChange = (page, pageSize) => {
  // console.log(page, pageSize)
  pagination.value.pageSize = pageSize
  searchParams.value.pageSize = pageSize
}
const pagination = ref<Pagination>({ page: 1, pageSize: 10, total: 0, onChange: handlePageChange, showQuickJumper: true })
let loading = ref<boolean>(false)

interface SearchParams {
  currentPage: number
  pageSize: number
  orderNumber?: string
  userName?: string
  robotId?: string
  changeType?: string
}
const searchParams = ref<SearchParams>({
  currentPage: pagination.value.page,
  pageSize: pagination.value.pageSize,
})

const dataSource = ref([])
const columns = [
  {
    title: '订单号',
    key: 'orderId',
    width: 350,
  },
  {
    title: '用户名',
    key: 'userName',
    width: 350,
  },
  {
    title: '变动金额 / 变动类型',
    key: 'changeType',
    width: 300,
  },
  {
    title: '变动前金额 / 变动后金额',
    key: 'changeBalance',
    width: 300,
  },
  {
    title: '变动时间',
    key: 'updateTime',
    width: 400,
    fixed: 'right',
  },
  // {
  //   title: '操作',
  //   key: 'action',
  //   width: 90,
  //   fixed: 'right',
  // },
]

// 翻页
const changePage = (e: any) => {
  pagination.value.current = e.current
  pagination.value.pageSize = e.pageSize

  searchParams.value.currentPage = pagination.value.current as number
  getList()
}

const getList = async () => {
  loading.value = true
  try {
    let {
      state,
      data,
      message: msg,
    } = await adminFinancialCenterApi.getUserBalanceRecordAdmin(searchParams.value)
    if (state === 200) {
      dataSource.value = data.list
      pagination.value.total = Number(data.totalCount)
    } else {
      message.error(msg)
    }
  } catch (error) {
    message.error('网络请求连接失败~')
  }
  loading.value = false
}

// 	变动类型
const typeList = ref<SelectProps['options']>([
  {
    label: '充值',
    value: '1',
  },
  // {
  //   label: '扣款',
  //   value: '2',
  // },
  {
    label: '订单解冻',
    value: '3',
  },
  {
    label: '后台操作加款',
    value: '4',
  },
  {
    label: '订单退款',
    value: '5',
  },
  {
    label: '订单扣款',
    value: '11',
  },
  {
    label: '订单冻结',
    value: '12',
  },
  {
    label: '后台操作扣款',
    value: '14',
  },
])

const compileType = (type: number | string) =>
  typeList.value?.find(item => item.value == type)?.label

onMounted(() => getList())

// 查询
const formRef = ref()
interface FormState {
  changeType?: string
  orderNumber?: string
  userName?: string
}
const formState = reactive<FormState>({})
const search = () => {
  pagination.value.current = 1
  searchParams.value.currentPage = 1
  formRef.value.validate().then(() => {
    if (formState.orderNumber) {
      searchParams.value.orderNumber = formState.orderNumber
    }
    if (formState.userName) {
      searchParams.value.userName = formState.userName
    }
    if (formState.changeType) {
      searchParams.value.changeType = formState.changeType
    }
    getList()
  })
}

// 重置
const reset = () => {
  pagination.value.current = 1
  searchParams.value.currentPage = 1

  formRef.value.resetFields()
  Reflect.deleteProperty(searchParams.value, 'orderNumber')
  Reflect.deleteProperty(searchParams.value, 'userName')
  Reflect.deleteProperty(searchParams.value, 'changeType')

  getList()
}
</script>

<template>
  <page-container :title="route.meta.title">
    <a-card style="overflow-x: scroll">
      <div class="searchBox">
        <a-form ref="formRef" layout="inline" :model="formState">
          <a-form-item label="订单编号" name="orderNumber">
            <a-input v-model:value="formState.orderNumber" placeholder="订单编号" />
          </a-form-item>

          <a-form-item label="用户名" name="userName">
            <a-input v-model:value="formState.userName" placeholder="用户名" />
          </a-form-item>

          <a-form-item label="变动类型" name="changeType">
            <a-select
              v-model:value="formState.changeType"
              placeholder="请选择"
              style="width: 150px"
              allowClear
            >
              <a-select-option v-for="(item, index) in typeList" :key="index" :value="item.value">
                {{ item.label }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-form>
        <a-button type="primary" style="border-radius: 6px" @click="search">
          <template #icon>
            <SearchOutlined />
          </template>
          搜索
        </a-button>
        <a-button @click="reset" style="border-radius: 6px">重置</a-button>
      </div>

      <a-alert
        description="小提示：只要鼠标在表格上，按住左Shift键并滚动鼠标滚轮就可以横向滚动表格"
        type="info"
        banner
        style="margin-bottom: 10px; padding: 10px"
      />

      <a-table
        style="overflow-x: scroll"
        :columns="columns"
        :data-source="dataSource"
        :pagination="pagination"
        :loading="loading"
        @change="changePage"
        rowKey="id"
        size="small"
        :scroll="{ x: 100 }"
      >
        <template #emptyText>
          <a-empty description="暂无数据" />
        </template>

        <template #headerCell="{ column }">
          <template v-if="column.key === 'id'">
            <span>id</span>
          </template>
        </template>

        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'orderId'">
            <a-descriptions bordered size="small" :column="1" :labelStyle="{ width: '100px' }">
              <a-descriptions-item label="平台ID">{{ record.id }}</a-descriptions-item>
              <a-descriptions-item label="订单编号">
                {{ record.orderNumber || '无' }}
                <a @click="copyToCliboard(record.orderNumber)"><CopyOutlined /></a>
              </a-descriptions-item>
            </a-descriptions>
          </template>

          <template v-if="column.key === 'robotId'">
            <div style="width: 150px">{{ record.robotId }}</div>
          </template>

          <template v-if="column.key === 'userName'">
            <a-descriptions bordered size="small" :column="1" :labelStyle="{ width: '100px' }">
              <a-descriptions-item label="用户名">
                <a-tag color="blue">{{ record.userName || '-' }}</a-tag>
                <a @click="copyToCliboard(record.userName)"><CopyOutlined /></a>
              </a-descriptions-item>
              <a-descriptions-item label="userId">{{ record.userId || '无' }}</a-descriptions-item>
            </a-descriptions>
          </template>

          <template v-else-if="column.key === 'changeType'">
            <a-descriptions bordered size="small" :column="1" :labelStyle="{ width: '100px' }">
              <a-descriptions-item label="变动金额">
                ￥{{ toYuan(record.changeBalance) }}
              </a-descriptions-item>
              <a-descriptions-item label="变动类型">
                <a-tag
                  color="red"
                  v-if="
                    record.changeType == '2' ||
                    record.changeType == '11' ||
                    record.changeType == '12'
                  "
                >
                  {{ compileType(record.changeType) }}
                </a-tag>
                <a-tag color="blue" v-else>{{ compileType(record.changeType) }}</a-tag>
              </a-descriptions-item>
            </a-descriptions>
          </template>

          <template v-else-if="column.key === 'changeBalance'">
            <a-descriptions bordered size="small" :column="1" :labelStyle="{ width: '110px' }">
              <a-descriptions-item label="变动前金额">
                ￥{{ toYuan(record.beforeBalance) }}
              </a-descriptions-item>
              <a-descriptions-item label="变动后金额">
                ￥{{ toYuan(record.afterBalance) }}
              </a-descriptions-item>
            </a-descriptions>
          </template>

          <template v-else-if="column.key === 'updateTime'">
            <a-descriptions bordered size="small" :column="1" :labelStyle="{ width: '110px' }">
              <a-descriptions-item label="变动时间">
                {{ timeFormat_2(record.updateTime) }}
              </a-descriptions-item>
              <a-descriptions-item label="变动备注">
                <a-tooltip placement="top">
                  <template #title>
                    <span>{{ record.changeRemark }}</span>
                  </template>
                  <span class="hiddenText">{{ record.changeRemark }}</span>
                </a-tooltip>
              </a-descriptions-item>
            </a-descriptions>
          </template>

          <template v-else-if="column.key === 'action'">
            <div style="width: 100px; margin: 0; padding: 0"></div>
          </template>
        </template>
      </a-table>
    </a-card>
  </page-container>
</template>

<style lang="less" scoped>
.hiddenText {
  display: inline-block;
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
  line-height: 16px;
}

.searchBox {
  margin-bottom: 10px;
  display: flex;

  input {
    width: 200px;
    margin-right: 10px;
  }

  button {
    margin-right: 10px;
  }
}

a:hover {
  color: red;
}
</style>
